﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Kernelx.Contexts.Defaults.Sequences._Internals
{

    using ParameterOutput = ParameterBracket.ParameterOutput;
    using SequeOutput = SequeBracket.SequeOutput;


    internal partial class SequeGenerator
    {
#if DEBUG
        /// <summary>
        /// 缓存unique及对应的当前序列值
        /// </summary>  
#endif
        internal class Seques
        {
            private readonly IDictionary<string, int> _container;

            public Seques() => _container = new Dictionary<string, int>();

            public bool Contains(string unique) => _container.ContainsKey(unique);

#if DEBUG
            /// <summary>
            /// 获取或设置unique对应的值
            /// </summary>
            /// <param name="unique"></param>
            /// <returns></returns>
#endif
            public int this[string unique] { get => _container[unique]; set => _container[unique] = value; }

        }


        internal class NormalOutputs
        {
            private readonly IDictionary<string, Output> _container = new Dictionary<string, Output>();
            private readonly IList<string> _args = new List<string>();

            public void Append(string name, Output output, bool hasArgs)
            {
                if (_container.ContainsKey(name))
                    throw new ApplicationException($"公式段{name}出现重复");
                _container.Add(name, output);
                if (hasArgs)
                    _args.Add(name);
            }

#if DEBUG
            /// <summary>
            /// 获取 规则名/生成值 的字典对象
            /// </summary>
            /// <returns></returns>  
#endif
            public IDictionary<string, string> Fetch() => _container.ToDictionary(kv => kv.Key, kv => kv.Value.Get());

            public IList<string> Args { get => _args; }


        }

#if DEBUG
        /// <summary>
        /// 描述target的序号生成所需的信息
        /// </summary>  
#endif
        internal class TargetOptions
        {
            public string Format { get; set; }

            public GetMaxID GetMaxID { get; set; }

            public SequeOutput SequeOutput { get; set; }
            public NormalOutputs NormalOutputs { get; private set; }
            public IList<ParameterOutput> ParameterOutputs { get; private set; }

            public Seques Seques { get; private set; }

            public TargetOptions()
            {
                NormalOutputs = new NormalOutputs();
                ParameterOutputs = new List<ParameterOutput>();
                Seques = new Seques();
            }

        }

    }

}
